<?php
namespace App\Library\Impl;
use App\Models\DawnOss;
use Illuminate\Support\Facades\Log;
use App\Library\UserLibraryInterface;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use App\Pojo\ResponsePojo;
use App\Library\ZhjyLibraryInterface;
use Illuminate\Support\Facades\Auth;
use App\Models\DawnZhjyUser;
use App\Exceptions\BusinessException;

class ZhjyLibraryImpl implements ZhjyLibraryInterface{

    private $dawn_zhjyId;

    public function __construct()
    {
        $this->dawn_zhjyId = Auth::id();
        if ($this->dawn_zhjyId === null) {
            // 处理用户未登录的情况
            // throw new BusinessException('用户未登录');
            Log::info('用户未登录');
        }
    }

    public function bindLogin($credentials){
        $existingUser = $this->findZhjyUserById();
        if(!$existingUser){
            // 未绑定， 进行绑定
            $this->inserBandInfo($credentials);
            return true;
        }else{
            // 已经绑定，更新数据
            $updateData = [
                'dawn_zhjyUser' => $credentials['dawn_zhjyUser'],
                'dawn_zhjyPassword' => $credentials['password'],
                'dawn_zhjyAvatar' => $credentials['avatar'],
                'dawn_zhjyEmail' => $credentials['email'],
                'dawn_zhjyPhone' => $credentials['phone'],
                'dawn_zhjyidEntity' => $credentials['userType'],
                'dawn_zhjyIdCar' => $credentials['username'],
                'dawn_zhjyIdName' => $credentials['nickName'],
                'dawn_zhjySex' => $credentials['sex'],
                'dawn_zhjyIStudentId' => $credentials['studentNo'],
                'dawn_zhjyDeptName' => $credentials['deptName'],
                'dawn_zhjyEnrollmentYear' => $credentials['enrollmentYear'],
                'dawn_zhjyUpdataTime' => now()
            ];
            
            Log::info('更新用户数据 - 当前时间: ' . now() . ' - 时区: ' . config('app.timezone'));
            // 使用dawn_id作为条件进行更新
            DawnZhjyUser::where('dawn_id', $this->dawn_zhjyId)->update($updateData);
            return true;
        }
    }

    /**
     * 查询绑定状态
     * @return void
     */
    public function findZhjyStatus(){

        return DawnZhjyUser::where("dawn_id", $this->dawn_zhjyId)->first();
    }

    /**
     * 登录信息入库
     */
    public function inserBandInfo($username){
        $data = [
            'dawn_id' =>  $this->dawn_zhjyId,
            'dawn_zhjyUser' => $username['dawn_zhjyUser'],
            'dawn_zhjyPassword' => $username['password'],
            'dawn_zhjyAvatar' => $username['avatar'],
            'dawn_zhjyEmail' => $username['email'],
            'dawn_zhjyPhone' => $username['phone'],
            'dawn_zhjyidEntity' => $username['userType'],
            'dawn_zhjyIdCar' => $username['username'],
            'dawn_zhjyIdName' => $username['nickName'],
            'dawn_zhjySex' => $username['sex'],
            'dawn_zhjyIStudentId' => $username['studentNo'],
            'dawn_zhjyDeptName' => $username['deptName'],
            'dawn_zhjyEnrollmentYear' => $username['enrollmentYear'],
            'dawn_zhjyCreateTime' => now(),
            'dawn_zhjyUpdataTime' => now(),
            'dawn_zhjyStatus' => 0
        ];
        return DawnZhjyUser::create( $data );
    }

    /**
     * 检查是否有重复 登录信息入库
     */
    public function findZhjyUserById(){
        return DawnZhjyUser::where("dawn_id", $this->dawn_zhjyId)->first();
    }

    /**
     * 保存文件信息
     * @return int 返回文件记录的主键ID
     */
    public function saveZhjyFile($credentials){
        Log::info($credentials);
        Log::info('保存文件信息 - 当前时间: ' . now() . ' - 时区: ' . config('app.timezone')); 
        $file = DawnOss::create($credentials);
        return $file; // 返回主键ID
    }

}